# -*- conding:UTF-8 -*-

'''
@ Project: P8_02_LumenIM-flask
@ File: common_app.py
@ Author: 罗婷
@ Date：2022/7/10 19:00

'''
import random

import redis
from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse, marshal

from common.celery.tasks import send_sms

common_bp = Blueprint('common_bp', __name__, url_prefix='/api/v1/common')
api = Api(common_bp)

# 发送短信验证码
class Send_sms(Resource):
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('mobile')
        args = parser.parse_args()
        mobile = args['mobile']
        code = f'{random.randint(1000,9999)}'
        res = send_sms(mobile, code)
        if res:
            print(f'短信验证码是{code}')
            rds = redis.Redis()
            rds.set(mobile, code, ex=60*2)
            return jsonify(message='短信发送成功', code=200)
        else:
            return jsonify(message='短信发送失败', code=400)



api.add_resource(Send_sms, '/sms-code')
